<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="组织名称">
              <a-input v-model="queryParam.name" placeholder="" />
            </a-form-item>
          </a-col>

          <!-- <a-col :md="6" :sm="24">
            <a-form-item label="成立日期">
              <a-date-picker v-model="queryParam.createTime" style="width: 100%" placeholder="请输入成立日期" />
            </a-form-item>
          </a-col> -->

          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="成立日期">
              <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.buildTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.buildTime_end"></j-date>
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 4) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
             
            </span>
          </a-col>

          <!-- <a-col :md="4" :sm="24">
            <a-button type="primary" @click="handleApply">申请新学生组织</a-button>
          </a-col> -->

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'Organization:add'">新增</a-button>
      <a-button type="primary" icon="download" v-has="'Organization:export'" @click="handleExportXls('校级学生组织')">导出</a-button>
      
      <a-button type="primary" icon="import" @click="visibleImport=true" v-has="'Organization:import'">导入</a-button>
      <XImport v-model="visibleImport" moduleName="orgStdGroup" @ok="searchQuery" @cancel="visibleImport=false"></XImport>

      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'Organization:batchdelete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'Organization:batchdelete'"><a-icon type="delete" />批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <s-table ref="table" size="default" :scroll="{x:true}" :columns="columns" :data="getData" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"  @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleReview(record)">查看</a>
        <a-popconfirm v-has="'Organization:delete'" style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleEdit(record)" v-has="'Organization:edit'">编辑</a>
        <a style="margin-left:10px" @click="handleJoin(record)" v-has="'Organization:join'" :disabled="moment(record.dismissTime).isBefore(new Date())">申请加入</a>
      </span>
    </s-table>

    <!-- 学生组织弹窗 -->
    <OrganizationModal ref="OrganizationModal" @ok="searchQuery"></OrganizationModal>

    <!-- 加入学生组织弹窗 -->
    <OrganizationMemberModal ref="OrganizationMemberModal" ></OrganizationMemberModal>

  </a-card>
</template>

<script>
import moment from 'moment'
import { getAction, postAction, deleteAction } from '@/api/manage'
import { getOrganizationList ,delOrganization,delOrganizations} from '@/api/api'
import {merge} from 'lodash'
import OrganizationModal from './modules/OrganizationModal'
import OrganizationMemberModal from './modules/OrganizationMemberModal'
import XImport from '@/components/extends/XImport.vue'
import STable from '@/components/table/'
import JDate from '@/components/jeecg/JDate.vue'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
export default {
  name: 'OrganizationMange',
  components: {
    JDate,
    STable,
    XImport,
    XSelectUserByDep,
    OrganizationModal,
    OrganizationMemberModal
  },
  mixins:[JeecgListMixin],
  data() {
    return {
      moment,
      visibleImport:false,
      disableMixinCreated:true,
      form: null,
      mdl: {},
      url:{
        list:'/org/stdGroup/list',
        add:'/org/stdGroup/add',
        edit:'/org/stdGroup/edit',
        delete:'/org/stdGroup/delete',
        deleteBatch:'/org/stdGroup/deleteBatch',
        exportXlsUrl:'/org/stdGroup/exportXls',
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        actStatus:10
      },
      // 表头
      columns: [
        {
          title: '学生组织名称',
          align: 'center',
          width: 180,
          ellipsis: true,
          dataIndex: 'name'
        },
        // {
        //   title: '所属部门',
        //   align: 'center',
        //   dataIndex: 'departName'
        // },
        {
          title: '负责人',
          align: 'center',
          width: 150,
          dataIndex: 'director'
        },
        {
          title: '指导老师',
          align: 'center',
          width: 150,
          dataIndex: 'instructor'
        },
        // {
        //   title: '学院',
        //   align: "center",
        //   width: 150,
        //   dataIndex: 'college'
        // },
        // {
        //   title: '年级',
        //   align: "center",
        //   width: 120,
        //   dataIndex: 'grade'
        // },
        // {
        //   title: '班级',
        //   align: "center",
        //   width: 120,
        //   dataIndex: 'clazz'
        // },
        {
          title: '成立时间',
          align: 'center',
          width:150,
          dataIndex: 'buildTime',
          sorter: true
        },
        {
          title: '解散时间',
          align: 'center',
          width:220,
          dataIndex: 'dismissTime',
        },
        {
          title: '操作',
          align: 'center',
          fixed:"right",
          // dataIndex: 'action',
          width: 220,
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      getData: parameter => {
        let params = Object.assign({},parameter, this.queryParam)
        params.name&&(params.name = `*${params.name}*`)
        return getAction(this.url.list,params).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {},
  methods: {
    
    // 添加
    handleAdd(){
      this.$refs.OrganizationModal.title = '添加学生组织'
      this.$refs.OrganizationModal.add();
    },
    // 申请
    handleApply(){
      this.$refs.OrganizationModal.title = '申请创建学生组织'
      this.$refs.OrganizationModal.apply({
        directorId: this.$store.getters.userInfo.id,
        director: this.$store.getters.userInfo.username,
      },{
        directorId:true
      });
    },
    // 编辑
    handleEdit(record){
      this.$refs.OrganizationModal.title = '编辑'
      this.$refs.OrganizationModal.edit(record,{directorId:false});
    },
    // 查看
    handleReview(record){
      this.$refs.OrganizationModal.review(record);
    },
    // 删除
    handleDelete(id){
      delOrganization({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    // 申请加入
    handleJoin(record){
      this.$refs.OrganizationMemberModal.apply({
        stdGroupId:record.id,
        stdGroupName:record.name,
        userId: this.$store.getters.userInfo.id,
        userName: this.$store.getters.userInfo.username,
      },{
        userId:true,
        stdGroupName:true
      });
    },
    batchDel: function () {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            delOrganizations({ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    searchReset() {
      this.queryParam = {
        actStatus:10
      }
      this.searchQuery();
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  },
  watch: {
    currentOrganization:{
      handler(nv,ov){
        
        console.log(nv)
      },
      deep:true
    }
  }
}
</script>
